package hk.cic.cwrs.bio.integration;

import android.annotation.SuppressLint;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <�@�y�ܥ\��²�z>
 * <�\��ԲӴy�z>
 * 
 * @author  Administrator
 * @version  [1.0.0.0, 2012-10-26]
 */
public class TimeUtil {
    public static final String format1="yyyy-MM-dd HH:mm:ss";
    public static final String format2="yyyy/MM/dd";
    public static final String format3="yyyyMMddHHmmSS";
    public static final String format4="yyyy-MM-dd";
    public static final String format5="yyyy/MM/dd HH:mm:ss";
    public static final String format6="HH:mm";
    public static final String format7="yyyy/MM/dd HH:mm";
    private static final String TAG = "TimeUtil";
    
    @SuppressLint("SimpleDateFormat")
    public static String getCunrrentDateString(){
        SimpleDateFormat formatter = new SimpleDateFormat(format1);
        String dateString = formatter.format(new Date());
        return dateString;
    }
    
    @SuppressLint("SimpleDateFormat")
    public static long String2Long(String date,String format) throws ParseException{
        SimpleDateFormat formatter=new SimpleDateFormat(format);
        long timeMiilis=formatter.parse(date).getTime();
        return timeMiilis;
    }
    
    @SuppressLint("SimpleDateFormat")
	public static String format(long timeMiss,String format)throws ParseException{
    	SimpleDateFormat formatter = new SimpleDateFormat(format);
    	String dateString = formatter.format(timeMiss);
		return dateString;
    }
    
    public static long stringDateTimeToLongTime(String strDateTime){
    	Log.i(TAG, "strDateTime:"+strDateTime);
    	if(14!=strDateTime.length()) return 0;
    	try{
	    	String strYear=strDateTime.substring(0,4);
	    	Log.i(TAG, "Year:"+strYear);
	    	String strMonth=strDateTime.substring(4,6);
	    	Log.i(TAG, "Month:"+strMonth);
	    	String strDay=strDateTime.substring(6,8);
	    	Log.i(TAG, "Day:"+strDay);
	    	String strHour=strDateTime.substring(8,10);
	    	Log.i(TAG, "Hour:"+strHour);
	    	String strMenite=strDateTime.substring(10,12);
	    	Log.i(TAG, "Menite:"+strMenite);
	    	String strSecond=strDateTime.substring(12,14);
	    	Log.i(TAG, "Second:"+strSecond);
	    	
	    	String dateTime=strYear+"-"+strMonth+"-"+strDay+" "+strHour+":"+strMenite+":"+strSecond;
	    	Log.i(TAG, "stringDateTimeToLongTime:"+dateTime);
	    	return String2Long(dateTime,format1);
    	}catch(Exception e){
    		e.printStackTrace();
    		return 0;
    	}
    }
    
    public static long getDiffDays(long lngLastUpdateDateTime, long lngCurrentSystemTime) {
        long day = 0;
        try {
	        String endDateStr=TimeUtil.format(lngCurrentSystemTime, TimeUtil.format4);
	        
	        String beginDateStr=TimeUtil.format(lngLastUpdateDateTime, TimeUtil.format4);
	        Log.i(TAG, "beginDateStr:"+beginDateStr);
	        Log.i(TAG, "endDateStr:"+endDateStr);
	        
	        SimpleDateFormat format = new SimpleDateFormat(
	                "yyyy-MM-dd");
	        Date beginDate;
	        Date endDate;
        
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime())
                    / (24 * 60 * 60 * 1000);
            // System.out.println("相隔的天数="+day);
        } catch (ParseException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return day;
    }
    
    public static long getDiffMinutes(long lngLastUpdateDateTime, long lngCurrentSystemTime) {
        long minutes = 0;
        try {
	        String endDateStr=TimeUtil.format(lngCurrentSystemTime, TimeUtil.format5);
	        
	        String beginDateStr=TimeUtil.format(lngLastUpdateDateTime, TimeUtil.format5);
	        Log.i(TAG, "beginDateStr:"+beginDateStr);
	        Log.i(TAG, "endDateStr:"+endDateStr);
	        
	        SimpleDateFormat format = new SimpleDateFormat(TimeUtil.format5);
	        Date beginDate;
	        Date endDate;
        
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            minutes = (endDate.getTime() - beginDate.getTime())
                    / (60 * 1000);
            // System.out.println("相隔的天数="+day);
        } catch (ParseException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return minutes;
    }
    /** 
     * 获取增加多少天的时间 
     *  
     * @return addDay - 增加多少天 
     */  
    public static Date getAddDayDate(int addDay) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.add(Calendar.DAY_OF_YEAR, addDay);  
        return calendar.getTime();  
    } 
    /** 
     * 获取增加多少小时的时间 
     *  
     * @return addDay - 增加多少消失 
     */  
    public static Date getAddMinutesDate(int addMinutes) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.add(Calendar.MINUTE, addMinutes);  
        return calendar.getTime();  
    }  
    /**
     * Date to long
     * @param mDate
     * @return
     */
    public static long dateToLong(Date mDate){
    	return mDate.getTime();
    }
}
